<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .canvas{
            border: 1px solid #000;
            margin: 0 auto;
            display: block;
        }
    </style>
</head>
<body>
    <canvas class="canvas" width="600" height="600">
        
    </canvas>

    <script>
        var canvas = document.querySelector(".canvas")
        var ctx = canvas.getContext("2d")
        var isDraw = false
        ctx.fillRect(0,0,600,600)
        // 鼠标按下时间
        canvas.addEventListener('mousedown',function(){
            /** @type {CanvasRenderingContext2D} */
            console.log(ctx.beginPath())
            isDraw = true
        })
        canvas.addEventListener('mouseup',function(){
            /** @type {CanvasRenderingContext2D} */
            // console.log(ctx.beginPath())
            isDraw = false
        })

        // 鼠标移动事件
        canvas.addEventListener('mousemove',function(e){
            if(!isDraw){
                return
            } else {
                var x = e.offsetX
                var y = e.offsetY
                ctx.lineTo(x,y)
                ctx.stroke()
            }
        })
    </script>
</body>
</html>